<template>
  <div class="fy-login">
    <h1>后台管理系统</h1>
    <div class="fy-login-form">
      <el-form :model="formData" :rules="rules" ref="submitForm" class="login-form">
        <el-form-item prop="user">
          <el-input type="text" v-model="formData.user"></el-input>
        </el-form-item>
        <el-form-item prop="psw">
          <el-input type="password" v-model="formData.psw"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="login-button" type="primary" @click.stop="handleSubmit('submitForm')">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="login-p">
        <p class="text">温馨提示</p>
        <p class="text">未登录过的新用户，自动注册</p>
        <p class="text">注册过的用户可凭账号密码登录</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      formData: {
        user: "",
        psw: "",
      },
      rules: {
        user:{
          required:true,
          message:'输入不能为空',
          trigger:"blur"
        },
        psw:{
          required:true,
          message:'输入不能为空',
          trigger:"blur"
        },
      },
    };
  },
  methods:{
    handleSubmit(formName){
      this.$refs[formName].validate(valid=>{
        if(valid) {
          this.$axios.post('/admin/login',{
            user_name:this.formData.user,
            password:this.formData.psw,
          }).then(res=>{
            console.log(res);
            if(res.status === 1){
              this.$message({
                message:"登录成功",
                type:"success",
              })
              this.$router.push({path:"dashboard"});
            } else {
              this.$message({
                message:"登录失败",
                type:"error",
              })
            }
          })
        } else {
          console.log('验证失败');
          return false;
        }
      })
    }
  },
  components:{},
};
</script>

<style lang="less">
@import url("../style/common.less");
.fy-login {
  .tlc();
  h1 {
    color: white;
    text-align: center;
    font-weight: 200;
    padding: 20px;
  }
  .fy-login-form {
    border-radius: 10px;
    padding: 10px;
    background: white;
    .wh(300px,300px);
    .login-form{
      width: 90%;
      margin: 0 auto;
      .login-button {
        width:100%;
      }
    }
  }
  .login-p{
    .text{
      text-align: center;
      color: red;
      font-size: 14px;
    }
  }
}
</style>
